from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase
from Products.Five.browser import BrowserView
from random import randint
from zope.component import getMultiAdapter
from Products.CMFPlone.utils import getToolByName

class LogoViewlet(ViewletBase):
    index = ViewPageTemplateFile('templates/logo.pt')

    def update(self):
        super(LogoViewlet, self).update()
        #import pdb;pdb.set_trace()
        self.setUpByWhereIAm(self.context.getLayout())
        self.portal_title = self.portal_state.portal_title()
    
    def setUpByWhereIAm(self, layout_):
        
        if layout_ == 'uriachpharma_view':
            self.navigation_root_url = self.portal_state.navigation_root_url()
            portal = self.portal_state.portal()
            logoName = portal.restrictedTraverse('base_properties').logoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_micetal_view' or 'micetal' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/micetal'
            logoName = portal.restrictedTraverse('base_properties').micetalLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_copalia_view' or 'copalia' in self.context.getPhysicalPath():
            #import pdb;pdb.set_trace()
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/copalia'
            logoName = portal.restrictedTraverse('base_properties').copaliaLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_rupafin_view' or 'rupafin' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/rupafin'
            logoName = portal.restrictedTraverse('base_properties').rupafinLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_silostar_view' or 'silostar' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/silostar'
            logoName = portal.restrictedTraverse('base_properties').silostarLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_disgren_view' or 'disgren' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/disgren'
            logoName = portal.restrictedTraverse('base_properties').disgrenLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_telo_view' or 'telo' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/telo'
            logoName = portal.restrictedTraverse('base_properties').teloLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_ferinject_view' or 'ferinject' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/ferinject'
            logoName = portal.restrictedTraverse('base_properties').ferinjectLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_caphosol_view' or 'caphosol' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/caphosol'
            logoName = portal.restrictedTraverse('base_properties').caphosolLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_alvesco_view' or 'alvesco' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/alvesco'
            logoName = portal.restrictedTraverse('base_properties').alvescoLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_vimovo_view' or 'vimovo' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/vimovo'
            logoName = portal.restrictedTraverse('base_properties').vimovoLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        elif layout_ == 'uriach_palladone_view' or 'palladone' in self.context.getPhysicalPath():
            portal = self.portal_state.portal()
            self.navigation_root_url = portal.absolute_url() + '/palladone'
            logoName = portal.restrictedTraverse('base_properties').palladoneLogoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        else:
            self.navigation_root_url = self.portal_state.navigation_root_url()
            portal = self.portal_state.portal()
            logoName = portal.restrictedTraverse('base_properties').logoName
            self.logo_tag = portal.restrictedTraverse(logoName).tag()
        
class uriachFileView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_file_view.pt")
    
    def test(self, bool, yes, no):
        #import pdb;pdb.set_trace()
        if bool:
            return yes
        else:
            return no
    
class uriachDirectFileView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_direct_file_view.pt")
    
    def setUriachFileContentFilter(self):
        myFilter = {}
        myFilter['portal_type'] = 'UriachFile'
        myFilter['review_state']=['published', 'external', 'web_published','internally_published']
        myFilter['sort_on']='getObjPositionInParent'
        return myFilter
    
    def test(self, bool, yes, no):
        #import pdb;pdb.set_trace()
        if bool:
            return yes
        else:
            return no

class uriachSearchView(BrowserView):
    
    render = ViewPageTemplateFile("templates/uriach_search_view.pt")
    
    def listChildFolders(self):
        catalog = getToolByName(self.context, 'portal_catalog')
        
        path = '/'.join(self.context.getPhysicalPath())
        
        return catalog(portal_type='Folder',
                       path=path, 
                       review_state=['published', 'external', 'web_published','internally_published'], 
                       sort_on='getObjPositionInParent',)
    
    def getPath(self):
        return '/'.join(self.context.getPhysicalPath())
    
    def test(self, value, trueVal, falseVal):
        """
            helper method, mainly for setting html attributes.
        """
        if value:
            return trueVal
        else:
            return falseVal
        
class uriachGenericProductView(BrowserView):
    
    def test(self, value, trueVal, falseVal):
        """
            helper method, mainly for setting html attributes.
        """
        if value:
            return trueVal
        else:
            return falseVal
        
    def listVendedoresDelMes(self):
        urltool = getToolByName(self.context, 'portal_url')
        portal_catalog = getToolByName(self.context, 'portal_catalog')
        
        #path = urltool.getPortalPath() + '/vendedores-del-mes'
        path = '/'.join(self.context.getPhysicalPath()) + '/vendedores-del-mes'
        #mypath = path+'/'+id
        mypath = path
        
        
        vendedores = portal_catalog.searchResults(portal_type = 'Image',
                                         path = mypath,
                                         sort_on='getObjPositionInParent')
        
        #self.context.plone_log('viewlets, l.35, path='+str(path))
        #self.context.plone_log('viewlets, l.36, folders='+str(folders))
        #import pdb;pdb.set_trace()
        if vendedores is None:
            return False
        else:
            return vendedores
    
    def listVendedoresResponsables(self):
        urltool = getToolByName(self.context, 'portal_url')
        portal_catalog = getToolByName(self.context, 'portal_catalog')
        
        #path = urltool.getPortalPath() + '/vendedores-responsables'
        path = '/'.join(self.context.getPhysicalPath()) + '/vendedores-responsables'
        #mypath = path+'/'+id
        #import pdb;pdb.set_trace()
        mypath = path
        
        
        vendedores = portal_catalog.searchResults(portal_type = 'Image',
                                         path = mypath,
                                         sort_on='getObjPositionInParent')
        
        #self.context.plone_log('viewlets, l.35, path='+str(path))
        #self.context.plone_log('viewlets, l.36, folders='+str(folders))
        #import pdb;pdb.set_trace()
        if vendedores is None:
            return False
        else:
            return vendedores
        
class uriachMicetalView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_micetal_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-micetal'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   

        
class uriachCopaliaView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_copalia_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-copalia'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   

class uriachRupafinView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_rupafin_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-rupafin'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   

class uriachSilostarView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_silostar_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-silostar'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   
  

class uriachDisgrenView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_disgren_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-disgren'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   
  

class uriachTeloView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_telo_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-telo'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   
  

class uriachFerinjectView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_ferinject_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-ferinject'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   
  

class uriachCaphosolView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_caphosol_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-caphosol'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
   
  

class uriachAlvescoView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_alvesco_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-alvesco'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
      

class uriachVimovoView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_vimovo_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-vimovo'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]
      

class uriachPalladoneView(uriachGenericProductView):
    
    __call__ = ViewPageTemplateFile('templates/uriach_palladone_view.pt')
    

    def listNews(self):
        context=self.context
        urltool = getToolByName(context, 'portal_url')
        portal_catalog = getToolByName(context, 'portal_catalog')
        contentFilter={}
        #path={}
        cur_path = '/'.join(context.getPhysicalPath())
        #camitotalcarpeta = urltool.getPortalPath() + '/' + 'noticias-micetal    '
        camitotalcarpeta = cur_path + '/actualidad/noticias-palladone'
        contentFilter['portal_type']="News Item"
        contentFilter['review_state']=['published','internally_published',]
        #contentFilter['query']=camitotalcarpeta
        contentFilter['path']= camitotalcarpeta

        contentFilter['sort_on']='effective'
        contentFilter['sort_order']='reverse'
        #import pdb;pdb.set_trace()
        
        results= portal_catalog.searchResults(contentFilter)
        return results[:4]